﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Models;
using net8.Models.XiTong;
using net8.Views.XiTong;
using net8.Views;
using System.ComponentModel.DataAnnotations;
using net8.Validation;
using SqlSugar;
using System.Collections.Generic;
using System.Collections;

namespace net8.Controllers.XiTong
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class XtLogController : ControllerBase
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public PageReturn<XTLog> GetPageList([FromQuery] LogQuery model)
        {
            int total = 0;
            List<XTLog> list = DbHelper.Db.Queryable<XTLog>()
                .WhereIF(!string.IsNullOrEmpty(model.Url), l => l.RequestUrl.Contains(model.Url))
                .WhereIF(model.Durtion > 0, l => l.Durtion >= model.Durtion)
                .WhereIF(!string.IsNullOrEmpty(model.Type), l => l.LogType == model.Type)
                .WhereIF(!string.IsNullOrEmpty(model.StartTime), l => l.StartTime >= SqlFunc.ToDate(model.StartTime))
                .ToPageList(model.PageIndex, model.PageSize, ref total);
            return new PageReturn<XTLog>() { Total = total, List = list };
        }

        [HttpGet]
        public List<List<dynamic>> GetLogTypeChart()
        {
            var data = DbHelper.Db.Queryable<XTLog>()
                .GroupBy(l => l.LogType)
                .Select(l => new
                {
                    l.LogType,
                    Count = SqlFunc.AggregateCount(l.LogType)
                }).ToList();

            List<List<dynamic>> list = [];
            foreach (var item in data)
            {
                list.Add([item.LogType, item.Count]);
            }

            return list;
        }

        [HttpGet]
        public List<List<dynamic>> GetDayLogTypeChart()
        {
            DateTime endDate = DateTime.Today;
            DateTime startDate = endDate.AddDays(-7);

            var data = DbHelper.Db.Queryable<XTLog>()
                .Where(l => l.StartTime >= startDate && l.StartTime < endDate)
                .GroupBy(l => new { l.LogType, l.StartTime.Date })
                .Select(l => new
                {
                    l.LogType,
                    l.StartTime.Date,
                    Count = SqlFunc.AggregateCount(l.LogType)
                }).ToList();

            List<List<dynamic>> list = [];

            List<dynamic> columns = ["product"];
            List<string> ltList = data.Select(l => l.LogType).Distinct().ToList();
            foreach (var item in ltList)
            {
                columns.Add(item);
            }
            list.Add(columns);
            
            for (DateTime start = startDate; start < endDate; start = start.AddDays(1))
            {
                List<dynamic> rows = [start.ToString("yyyy-MM-dd")];
                foreach (var item in ltList)
                {
                    rows.Add(data.Where(l => l.LogType == item && l.Date == start).Sum(l => l.Count));
                }
                list.Add(rows);
            }

            return list;
        }
    }

    /// <summary>
    /// 日志查询参数
    /// </summary>
    public class LogQuery : PageQuery
    {
        public string Type { get; set; }
        public string Url { get; set; }
        public int Durtion { get; set; }

        public string StartTime { get; set; }
    }

}
